/**
 * 							TASK STRUCTURE DEFINE
 * @brief	task structure define
 * @author	chy.
 * @note	system mode stack of each thread should be defined ahead of thread structure
 * 			cause, we will use it in asm interrupt process!
 * @comment	we support multi-task and multi-thread!
 */

#ifndef _KERNEL_TASK_DEF_H_
#define _KERNEL_TASK_DEF_H_

#include "../mem/k_mm_heap.h"

 /**
 * Thread Priority
 */
typedef enum
{
	K_TASK_THREAD_PRIORITY_LOWEST	= 0x80000001,
	K_TASK_THREAD_PRIORITY_LOW      = 0x80000002,
	K_TASK_THREAD_PRIORITY_NORMAL	= 0x80000003,
	K_TASK_THREAD_PRIORITY_HIGHT	= 0x80000004,
	K_TASK_THREAD_PRIORITY_HIGHTEST = 0x80000005
}K_EN_TASK_PRIORITY;

/**
 * Process Type
 */
typedef enum
{
	K_TASK_PROCESS_TYPE_USR = 0x80000000,
	K_TASK_PROCESS_TYPE_SYS
}K_EN_PROCESSTYPE;

/**
 *	Thread Type (Main thread or other)
 */
typedef enum
{
	K_TASK_THREAD_TYPE_MAIN = 0x80000000,
	K_TASK_THREAD_TYPE_OTHER
}K_EN_THREADTYPE;

/**
*	Thread state
*/
typedef enum
{
	K_TASK_THREAD_STATE_TERMINATE = 0x80000000,
	K_TASK_THREAD_STATE_NEW		  = 0x80000001,
	K_TASK_THREAD_STATE_RUNNING	  = 0x80000002,
	K_TASK_THREAD_STATE_SUSPEND   = 0x80000004,
	K_TASK_THREAD_STATE_ACTIVE    = 0x80000008
}K_EN_THREAD_STATE;

/**
*	Process state
*/
typedef enum
{
	K_TASK_PROCESS_STATE_TERMINATE = 0x81000000,
	K_TASK_PROCESS_STATE_RUNNING = 0x81000001,
	K_TASK_PROCESS_STATE_SUSPEND = 0x81000002,
	K_TASK_PROCESS_STATE_ACTIVE  = 0x81000004,
	K_TASK_PROCESS_STATE_WAITING = 0x81000008
}K_EN_PROCESS_STATE;

/**
 * 	thread structure
 */
typedef	struct k_sys_tag_thread
{
	unsigned int *p_thread_sys_init_top;// sys mod stack initialize top
	unsigned int *p_thread_sys_stack;	// stack memory pointer.in little edian is bottom!
	unsigned int *p_thread_usr_stack;	// stack memory pointer.in little edian is bottom!
	unsigned int cpu_reg_sp;            // important!System mode stack top pointer! SP_sys!
	unsigned int sched_count;			// thread scheduled count
	unsigned int sched_endcnt;			// scheduled count when terminate thread
	void		*parent_process;		// process of thread(not list!)
	void		*parent_tcblist;		// point to tcb elements which including it
	unsigned int times;			        //
	K_EN_THREADTYPE thread_type;		// indicate whether this thread is the main thread of process
	K_EN_TASK_PRIORITY priority;		// thread priority
	K_EN_TASK_PRIORITY cur_pri;		    // thread current priority
	K_EN_THREAD_STATE  state;		    //
}K_Thread, *K_PThread;

/**
 * Thread List
 */
typedef struct k_sys_tag_threadlist
{
	K_PThread			thread;         // the first must be null
	struct k_sys_tag_threadlist	*next;	// next thread in the same process
}K_ThreadList,*K_PThreadList;

/**
 * Process structure
 */
typedef struct k_sys_tag_process
{
	K_ThreadList		suspend_threads;//
	K_ThreadList		active_threads;	//
	K_PThreadList       thread_running; //
	unsigned int        fsttb_phy_addr;	//
	unsigned int		sched_count;	// process scheduled count
	unsigned int		sched_endcnt;	// scheduled count when terminate process
	K_HEAPPOOL          proc_heappool;  //
	K_EN_TASK_PRIORITY  priority;		// process priority
	K_EN_TASK_PRIORITY  cur_pri;	    // process current priority
	K_EN_PROCESSTYPE    proctype;		//
	K_EN_PROCESS_STATE  state;		    //
	void				*parent_proclist;	// PCB of current process
}K_Process, *K_PProcess;

/**
 * Process List
 */
typedef struct k_sys_tag_processlist
{
	K_PProcess	process;                // the first must be null
	struct k_sys_tag_processlist *next;	// next prcess
}K_ProcessList, *K_PProcessList;

/** make 4KB bound **/
#define	K_BOUND_4KB(__val__)		__val__=(__val__+((1 << 12) - 1))&(~((1 << 12) - 1))

typedef unsigned int K_HPROCESS;        // process handle
typedef unsigned int K_HTHREAD;         // thread handle

#endif /* _KERNEL_TASK_DEF_H_ */
